<template>
	<view class="content">
		<view class="hei20"></view>
		<swiper class="wid90 mar borRad20" style="height: 430rpx;" circular :autoplay='true'>
			<swiper-item v-for="(item,index) in bannerList" :key="index">
				<image :src="item.banner_img" class="wh100 borRad20" mode="scaleToFill"></image>
			</swiper-item>
		</swiper>
		<view v-if="jianjie" class="wid90 mar bacFFF borRad20"
			style="box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.1);padding: 15px;width: 83%;margin-top: 10px;">
			<rich-text :nodes="jianjie"></rich-text>
		</view>
		<view class="dis disAl disJuC wid100" style="height: 300rpx;">
			<image src="../../static/butt.png" @click="daka(1)" style="width: 300rpx;" class="hei100"></image>
		</view>
		<!-- <button class="mar-top40 mar borRad50 coFFF foSi40 fowe600 mar dis disAl disJuC" @click="daka(1)"
			style="height: 300rpx;width: 300rpx;background-color: #00AD27;">一键获取激活码</button> -->
		<view class="wid90 mar">
			<rich-text :nodes="tips"></rich-text>
		</view>
		<view class="wid90 mar mar-top40">
			<ad unit-id="adunit-babe4c5af5c232e3" ad-intervals="30"></ad>
		</view>
		
		<view v-if="duihuanma" class="wh100 poAbs zhezhao">
			<view class="wh100 dis disAl disJuC">
				<view style="width: 550rpx;">
					<view class="wid100 poRel" style="height: 400rpx;">
						<uni-icons class="poAbs" style="top: 10rpx;right: 10rpx;z-index: 10;" 
							type="close" color="#000" size="45rpx" @click="duihuanma = false"></uni-icons>
						<image :src="code_img" class="wh100" style="border-radius:  20rpx 20rpx 0 0;"
							mode="scaleToFill"></image>
					</view>
					<view class="bacFFF" style="border-radius: 0 0 20rpx 20rpx;padding: 10rpx 0rpx 50rpx 0rpx;">
						<!-- <view class="textCen foSi35 wid90 mar">{{fuzhi}}</view> -->
						<rich-text :nodes="fuzhi"></rich-text>
						<view class="textCen foSi35 wid90 mar">{{code}}</view>
						<view class="mar mar-top30 hei60 dis disAl disJuC foSi30 coFFF borRad40"
							@click.stop="fuzhiClcik()" style="width: 300rpx;background-color: #00AD27;">复制</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				duihuanma: false,
				code: '',
				fuzhi: '',
				isLoading: false,
				bannerList: [],
				tips: '',
				rewardedVideoAd: null,
				jianjie: '',
				code_img: '',
				fx: '',
				index: 0,
				mf: 0,
				mfsj: '',
				mianfei: 0,
				mianfei_num: 0
			}
		},
		onLoad() {
			if(wx.createRewardedVideoAd){
				this.rewardedVideoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-dc88ec13a4efe0ef'
				})
				this.rewardedVideoAd.onLoad(() => {
					console.log('onLoad event emit')
					if(this.index == 1){
						this.isLoading = true
					}
				})
				this.rewardedVideoAd.onError((err) => {
					console.log('onError event emit', err)
				})
				this.rewardedVideoAd.onClose((res) => {
					console.log('关闭')
					// 用户点击了【关闭广告】按钮
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						this.getdhm()
						// if (this.isLoading) {
						// 	this.getdhm()
						// }
					} else {
						// 播放中途退出，不下发游戏奖励
						this.isLoading = false
					}
				})
			}
			this.getPeizhi()
			this.getNeirong()
			wx.showShareMenu({
				withShareTicket: true,
				menus: ["shareAppMessage", "shareTimeline"]
			})
		},
		onShareAppMessage(res) {
			let that = this;
			let imageUrl = that.fx.zf_coverimg || '';
			return {
				title: that.fx.zf_title,
				path: '/pages/index/index',
				imageUrl: imageUrl,
				desc: that.fx.zf_brief
			};
		},
		// 分享到朋友圈
		onShareTimeline() {
			let that = this;
			let imageUrl = that.fx.zf_coverimg || '';
			return {
				title: that.fx.zf_title,
				path: '/pages/index/index',
				imageUrl: imageUrl
			};
		},
		methods: {
			isToday(str){
				if(!str){
					return false;
				}
			    var d = new Date(str);
			    var todaysDate = new Date();
			    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
			        return true;
			    } else {
			        return false;
			    }
			},
			daka(index) {
				this.mfsj = uni.getStorageSync('mfsj')
				this.mf = uni.getStorageSync('mf')
				var pd = !this.isToday(this.mfsj)
				console.log(this.mfsj)
				console.log(this.mf)
				console.log(pd)
				if(pd && this.mianfei == 1){
					uni.setStorageSync('mf',0)
					uni.setStorageSync('mfsj',new Date())
					this.getdhm()
				}else if(this.mf < this.mianfei_num && this.mianfei == 1){
					uni.setStorageSync('mf',this.mf + 1)
					this.getdhm()
				}else{
					this.index = index
					this.rewardedVideoAd.show().catch(err => {
						console.log('激励视频 广告显示失败')
						this.isLoading = false
						uni.showToast({
							title: '您今天没有观看次数了',
							//将值设置为 success 或者直接不用写icon这个参数
							icon: 'error',
							//显示持续时间为 2秒
							duration: 2000
						})
					})
				}
			},
			fuzhiClcik() {
				var that = this
				uni.setClipboardData({
					data: that.code.toString(),
					success: function() {
						console.log('success');
						that.duihuanma = false
					},
					fail(res) {
						console.log(res)
					}
				});
			},
			getPeizhi() {
				var that = this;
				uni.request({
					url: that.global.url + 'api/zy/get_popup',
					method: "POST",
					header: {},
					data: {},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							uni.setNavigationBarTitle({
								title: result.data.gzh_name
							});
						} else {
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					}
				})
			},
			getNeirong() {
				var that = this;
				uni.request({
					url: that.global.url + 'api/zy/index_data',
					method: "POST",
					header: {},
					data: {},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.bannerList = result.data.banner
							var tips = result.data.tips
							tips = tips.replace(/<img/gi, "<img class='richImg'");
							that.tips = tips
							that.code_img = result.data.code_img
							that.fx = result.data.zf_data
							var data = result.data.index_brief
							data = data.replace(/<img/gi, "<img class='richImg'");
							that.jianjie = data
							that.mianfei =  result.data.mianfei
							that.mianfei_num = result.data.mianfei_num
						} else {
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					}
				})
			},
			getdhm() {
				var that = this
				uni.request({
					url: that.global.url + 'api/zy/get_codes',
					method: 'POST',
					header: {},
					data: {},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.fuzhi = result.data.strate
							that.code = result.data.codes
							that.isLoading = false
							that.duihuanma = true
						}
					}
				})
			}
		}
	}
</script>

<style>
	.content {}

	.richImg {
		width: 100% !important;
		height: auto !important;
	}

	.jingshi {
		color: #8a6d3b;
		background-color: #fcf8e3;
		padding: 5px 10px;
		border-radius: 6px;
	}
</style>